<template>
	<view class="sign">
		<view class="title">
			<view class="location" v-if="signType=='1'">{{loactionText}}</view>
			<view class="scan" v-if="signType=='0'">{{scanText}}</view>
		</view>

		<view class="button">
			<van-button class="cirlce-sign-button" color="linear-gradient(to right, #4bb0ff, #6149f6)"
				@click="signInClick">签到</van-button>

			<van-notify id="van-notify" />
			<van-toast id="van-toast" />
		</view>
	</view>
</template>

<script>
	import {
		signLocation,
		signScore
	} from '@/config/api.js';
	import {
		formatDate
	} from '@/utils/formateDate.js'
	import {
		mapState,
		mapGetters,
		mapActions
	} from 'vuex'
	import Notify from '../../wxcomponents/@vant/weapp/notify/notify';
	import Toast from '../../wxcomponents/@vant/weapp/toast/toast';
	export default {
		data() {
			return {
				actId: '',
				signType: '',
				scanText: '点击签到',
				loactionText: '点击获取定位签到'
			}
		},
		computed: {
			...mapGetters([
				'name',
				'account'
			])
		},
		methods: {
			signInClick() {
				let that = this
				if (this.signType == '0') {
					// 为扫码签到
					signScore({
						actId: that.actId,
						userAccount: that.account,
						signTime: formatDate()
					}).then(scanRes => {
						if (scanRes.hasOk) {
							Toast.success(scanRes.message);
							setTimeout(() => {
								uni.navigateBack({
									delta: 1, // 返回页面数，1表示返回上一页，2表示返回上上一页，依次类推
								});
							}, 2000);

						} else {
							that.scanText = scanRes.message
						}
					}).catch(err => {
						console.log(err)
					})

				} else if (this.signType == '1') {
					// 为定位签到
					uni.getLocation({
						type: "gcj02",
						success: async (res) => {
							const LoactionRes = await signLocation({
								actId: that.actId,
								userLatitude: res.latitude,
								userLongitude: res.longitude,
								userAccount: that.account,
								signTime: formatDate()
							})
							if (LoactionRes.hasOk) {
								Toast.success(LoactionRes.message);
								setTimeout(() => {
									uni.navigateBack({
										delta: 1, // 返回页面数，1表示返回上一页，2表示返回上上一页，依次类推
									});
								}, 2000);
							} else {
								that.loactionText = LoactionRes.message
							}
						}
					})
				}
			},
		},
		onLoad: function(option) {
			this.actId = option.actId
			this.signType = option.signType
			console.log(this.signType);
		}
	}
</script>

<style lang="scss" scoped>
	.sign {
		width: 100%;
		height: 100%;
		position: relative;
	}

	.title {
		position: absolute;
		top: 15%;
		width: 100%;
		font-size: 24px;
		font-weight: 700;
		text-align: center;
	}

	.button {
		position: absolute;
		bottom: 40%;
		left: 50%;
		margin-left: -50px;

		.cirlce-sign-button {
			/deep/.van-button {
				width: 100px;
				height: 100px;
				border-radius: 50%;
				text-align: center;
			}
		}
	}
</style>